package com.wudi.estate.service.impl.servicemode;

import com.wudi.estate.bean.assetmanage.Device;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.servicemode.Complain;
import com.wudi.estate.mapper.assetmanage.DeviceMapper;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.homemanage.BuildingsMapper;
import com.wudi.estate.mapper.ownermanagement.PersonnelMapper;
import com.wudi.estate.mapper.servicemode.ComplainMapper;
import com.wudi.estate.service.servicemode.ComplainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ComplainServiceImpl implements ComplainService {
    @Autowired
    private ComplainMapper complainMapper;
    @Autowired
    private PersonnelMapper personnelMapper;
    @Autowired
    private BuildingsMapper buildingsMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    //查询所有投诉（查询投诉用）
    @Override
    public List<Map<String,Object>> selectAll() throws Exception {
        List<Map<String,Object>> lists = new ArrayList<>();
        List<Complain> complains = complainMapper.selectList(null);
        for (Complain complainAll : complains) {
            Map<String,Object> maps = new HashMap<>();
//            根据i投诉d获取投诉的信息
            Complain complainById = complainMapper.selectById(complainAll.getId());
            maps.put("complainById",complainById);
//            根据投诉的pid获取personnel的信息
            Personnel personnelByPid = personnelMapper.selectById(complainById.getPid());
            maps.put("personnelByPid",personnelByPid);
//            根据personnel的bid获取Buildings的信息
            Buildings buildingsByBid = buildingsMapper.selectById(personnelByPid.getBid());
//            根据Buildings的cid获取小区的信息；
            Community communityByCid = communityMapper.selectById(buildingsByBid.getCid());
            maps.put("communityByCid",communityByCid);
//            根据complain的Did查询设备的信息；
            Device deviceByDid = deviceMapper.selectById(complainAll.getDid());
            maps.put("deviceByDid",deviceByDid);
            lists.add(maps);
        }
        return lists;
    }
    //    查询投诉和小区和人员到一个集合（显示添加用）
    @Override
    public Map<String, Object> selectComplainAndCommunityAndPersonnel() throws  Exception {
        Map<String,Object> maps = new HashMap<>();
        List<Complain> complains = complainMapper.selectList(null);
        List<Community> communities = communityMapper.selectList(null);
        List<Personnel> personnels = personnelMapper.selectList(null);
        List<Device> devices = deviceMapper.selectList(null);
        maps.put("devices",devices);
        maps.put("communities",communities);
        maps.put("personnels",personnels);
        return maps;
    }
   //    添加一个投诉（添加用）
    @Override
    public void insert(Complain complain) throws Exception {
        complainMapper.insert(complain);
    }
    //根据id查询投诉和人员及设备（编辑显示用）
    @Override
    public Map<String, Object> selectComplainAndPersonnelAndDeviceById(long id) throws Exception {
        Map<String,Object> maps = new HashMap<>();
        Complain complain = complainMapper.selectById(id);
        maps.put("complain",complain);
        List<Community> communities = communityMapper.selectList(null);
        List<Personnel> personnels = personnelMapper.selectList(null);
        List<Device> devices = deviceMapper.selectList(null);
        maps.put("devices",devices);
        maps.put("communities",communities);
        maps.put("personnels",personnels);
        return maps;
    }
    //    根据id更改投诉（编辑用）
    @Override
    public void update(Complain complain) {
        complainMapper.updateById(complain) ;
    }
    //  把指定的id的状态更改（修改用）
    @Override
    public void deleteById(long id) throws Exception {
        Complain complain = complainMapper.selectById(id);
        complain.setState(false);
        complainMapper.updateById(complain);
    }
}
